var nbcgd = {
    initQuery: function () {
        $('#cgd_cgdw').combotree({
            loadFilter: function (rows) {
                return convert(rows);
            },
            onSelect: function (record) {
                $('#cgd_cgr').combobox({
                    url: $.webPath() + '/sys/yg/ygXmList?zgjgId=' + record.id,
                    valueField: 'id',
                    textField: 'xm'
                });
            }
        });
    },
    initDatagrid: function () {
        $("#grid_nbcgd").datagrid(nbcgd.dataOptions);
    },
    initBtn: function () {
        $("#btn_nbcgd_add").click(function () {
            nbcgd.showWindow({"htType": "nbcg"});
        });
        $("#btn_cancel_nbcgd").click(function () {
            var row = $('#grid_nbcgd').datagrid('getSelected');
            if (row) {
                $.messager.confirm('确认', '是否取消提交此送货单', function (r) {
                    if (r) {
                        var index = $('#grid_nbcgd').datagrid('getRowIndex', row);
                        var newRow = row;
                        newRow["zt"] = "ybc";
                        newRow["zt_text"] = "已保存";
                        $.post($.webPath() + '/cggl/nbcgd/cgd_cancel', {"cgd.id": row.id}, function (data) {
                            var obj = eval('(' + data + ')');
                            if (obj.statusCode == '200') {
                                $('#grid_nbcgd').datagrid('updateRow', {"index": index, row: newRow});
                                nbcgd.setBtnStatus();
                                $.messager.show({title: '提示框', msg: obj.message, timeout: 3000, showType: 'slide'});
                            }
                            else {
                                $.messager.alert('提示', obj.message, 'info');
                            }
                        });
                    }
                });
            } else {
                $.messager.alert('提示', "请选择要取消的送货单", 'info');
            }
        });
        $("#btn_nbcgd_mod").click(function () {
            nbcgd.showWindow({"cgd.id": $('#grid_nbcgd').datagrid('getSelected').id,
                "htType": $('#grid_nbcgd').datagrid('getSelected').cglx});
        });
        $("#btn_nbcgd_del").click(function () {
            var row = $('#grid_nbcgd').datagrid('getSelected');
            if (row) {
                $.messager.confirm('确认', '是否确认删除此送货单', function (r) {
                    if (r) {
                        var index = $('#grid_nbcgd').datagrid('getRowIndex', row);
                        $.post($.webPath() + '/cggl/nbcgd/cgd_del', {"cgd.id": row.id}, function (data) {
                            var obj = eval('(' + data + ')');
                            if (obj.statusCode == '200')
                                $('#grid_nbcgd').datagrid('deleteRow', index);
                            else
                                $.messager.alert('提示', obj.message, 'info');
                        });
                    }
                });
            } else {
                $.messager.alert('提示', "请选择要删除的送货单", 'info');
            }
        });
        $("#btn_nbcgd_confirm").click(function () {
            var row = $('#grid_nbcgd').datagrid('getSelected');
            if (row) {
                $.messager.confirm('确认', '是否提交此送货单', function (r) {
                    if (r) {
                        var index = $('#grid_nbcgd').datagrid('getRowIndex', row);
                        var newRow = row;
                        newRow["zt"] = "ytj";
                        newRow["zt_text"] = "已提交";
                        $.post($.webPath() + '/cggl/nbcgd/cgd_confirm', {"cgd.id": row.id}, function (data) {
                            var obj = eval('(' + data + ')');
                            if (obj.statusCode == '200') {
                                $('#grid_nbcgd').datagrid('updateRow', {"index": index, row: newRow});
                                nbcgd.setBtnStatus();
                                $.messager.show({title: '提示框', msg: obj.message, timeout: 3000, showType: 'slide'});
                            }
                            else {
                                $.messager.alert('提示', obj.message, 'info');
                            }
                        });
                    }
                });
            } else {
                $.messager.alert('提示', "请选择要的送货单", 'info');
            }
        });
        //查询
        $("#btn_nbcgd_search").click(function () {
            $('#grid_nbcgd').datagrid({queryParams: nbcgd.getQuery()});
        });
    },
    setBtnStatus: function () {
        var row = $("#grid_nbcgd").datagrid("getSelected");
        if (row && row.id && row.zt == "ybc") {
            $("#btn_nbcgd_mod,#btn_nbcgd_del,#btn_nbcgd_confirm").show();
            $("#btn_nbcgd_stock").hide();
            $("#btn_cancel_nbcgd").hide();
        } else if (row && row.id && row.zt == "ytj") {
            $("#btn_nbcgd_mod,#btn_nbcgd_del,#btn_nbcgd_confirm").hide();
            $("#btn_nbcgd_stock").show();
            $("#btn_cancel_nbcgd").show();
        } else {
            $("#btn_nbcgd_mod,#btn_nbcgd_del,#btn_nbcgd_confirm,#btn_nbcgd_stock #btn_cancel_nbcgd").hide();
        }
    },
    dataOptions: {
        url: $.webPath() + "/cggl/nbcgd/cgdlist?htType=nbcg&sftzd=false",
        pageNumber: 1,
        onDblClickCell: function (index, field) {
            if (field == "cgdh") {
                $("#btn_nbcgd_mod:visible").click();
            }
        },
        loadFilter: function (data) {
            $(data.rows).each(function () {
                this.zt_text = this.zt == 'ysp' ? "已审批" :
                    this.zt == 'ybc' ? "已保存" :
                        this.zt == 'ytj' ? "已提交" : "未知";
            })
            return data;
        },
        onLoadSuccess: function () {
            $(this).datagrid('selectRow', 0);
            nbcgd.setBtnStatus();
        },
        onClickRow: function () {
            nbcgd.setBtnStatus();
        },
        columns: [
            [
                {field: 'cgdh', title: '送货单号', width: 35, formatter: nbcgdCell},
                {field: 'cglx', title: '采购类型', formatter: function (value) {
                    if (value == 'nbcg')
                        return '内部采购';
                }, width: 17},
                {field: 'fhrq', title: '发货日期', width: 23},
                {field: 'gys_jc', title: '供应商名称', width: 40},
                {field: 'cgsl', title: '采购数量',
                    formatter: function (value) {
                        var sl = formatSlForHtml(value);
                        return sl;
                    }, width: 40},
                {field: 'cgje', title: '采购总金额(元)',
                    formatter: function (value) {
                        var sl = formatJeForHtml2(value);
                        return sl;
                    }, width: 45},
                {field: 'glfyje', title: '物流费用(元)',
                    formatter: function (value) {
                        var sl = formatJeForHtml2(value);
                        return sl;
                    }, width: 45},
                {field: 'cggs_jc', title: '采购公司', width: 23},
                {field: 'sbgs_jc', title: '申报公司', width: 23},
                {field: 'sbbm_qc', title: '申报部门', width: 15},
                {field: 'sbr_xm', title: '申报人', width: 13},
                {field: 'createUser_xm', title: '制单人', width: 13},
                {field: 'zt_text', title: '状态', width: 13}
            ]
        ]
    },
    showWindow: function (cgddata) {
        var tit = cgddata["cgd.id"] ? "修改内部采购单" : "添加内部采购单";
        nbcgd.win_cgd = $.window({
            title: tit,
            iconCls: 'icon-add2',
            width: 1024,
            height: 500,
            cache: false,
            url: $.webPath() + "/cggl/nbcgd/nbcgd_content",
            data: cgddata,
            buttons: [
                {
                    text: '保存',
                    handler: nbcgd_add
                }
            ]
        });
    },
    showStockWindow: function (cgddata) {
        nbcgd.win_cgd = $.window({
            title: "入库审核",
            iconCls: 'icon-add',
            width: 1024,
            height: 500,
            cache: false,
            url: $.webPath() + "/cggl/nbcgd/nbcgd_stock",
            data: cgddata,
            buttons: [
                {
                    text: '保存',
                    handler: cgd_stock
                }
            ]
        });
    },
    win: {
    },
    showDetail: function (id) {
        if ($("#layout_nbcgd #cgd_id").val() != id) {
            nbcgd.alwayshowDetail(id);
        }
    },
    alwayshowDetail: function (id) {
        $('#layout_nbcgd').layout('remove', 'center');
        $('#layout_nbcgd').layout('add', {
            region: 'center',
            split: true,
            href: $.webPath() + '/cggl/nbcgd/nbcgd_detail?cgd.id=' + id
        });
    },
    getQuery: function () {
        return {
            "zt": $("#nbcgd_zt").val(),
            "cglx": $("#nbcgd_cglx").val(),
            "cgdwId": $("#nbcgd_cgdw").combotree('getValue'),
            "cgr": $("#nbcgd_cgr").val(),
            "fhrqStart": $('#nbcgd_zdsj_start').datebox('getValue'),
            "fhrqEnd": $('#nbcgd_zdsj_end').datebox('getValue'),
            "cgdh": $("#nbcgd_cgdh").val()
        }
    },
    selectedRowId: null
}

$(function () {
    //查询条件-加载
    nbcgd.initQuery();
    //按钮-加载
    nbcgd.initBtn();
    //列表-加载
    nbcgd.initDatagrid();
})

function nbcgd_stock() {
    $("#cgd_mx").edatagrid('saveRow');
    $("#nbcgdForm").form('submit', {
        url: $.webPath() + "/cggl/nbcgd/cgd_stock",
        onSubmit: function (param) {
            var data = $('#cgd_mx').edatagrid("getData");
            if (!data.rows.length) {
                $('#nbcgdForm').tabs('select', "送货明细");
                $.messager.alert("提示", "送货明细需要填写!", "waring");
                return false;
            }
            var arrayPrefix = 'cgd.cgdmxList';
            //物资计划明细
            var paramString = "{";
            //物资计划明细
            for (var i = 0; i < data.rows.length; i++) {
                if (data.rows[i]['cd.id'] == null || data.rows[i]['cd.id'] == '') {
                    $('#nbcgdForm').tabs('select', "送货明细");
                    $.messager.alert('提示', '第' + (i + 1) + '行的产地不能为空 !', 'info');
                    return false;
                } else if (data.rows[i].js == 0 || data.rows[i].js == null) {
                    $('#nbcgdForm').tabs('select', "送货明细");
                    $.messager.alert('提示', '第' + (i + 1) + '行的件数不能为0 !', 'info');
                    return false;
                }
                {
                    var objectString = $.toJSONStringSubPrefix(data.rows[i], arrayPrefix + "[" + i + "].",
                        [
                            'id',
                            ['wzgg.id', 'gg.id'],
                            'cd.id',
                            'jzfs.id',
                            'hsj',
                            'bhsj',
                            'bz',
                            'sbsl',
                            'js',
                            'rksl',
                            'rkjs',
                            'rkjz',
                            'kh.khbm'
                        ]);
                    paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                }
            }
            if (data.rows && data.rows.length > 0)
                paramString = paramString.substring(0, paramString.length - 1);
            paramString += "}";
            $.extend(param, $.parseJSON(paramString));
            return $(this).form('validate');
        },
        success: function (data) {
            var obj = eval('(' + data + ')');
            if (obj.statusCode == '200') {
                $(nbcgd.win_cgd).window('close');
                $('#grid_nbcgd').datagrid('reload');
                $('#layout_nbcgd').layout('panel', 'center').panel('refresh');
                $.messager.show({title: '提示框', msg: obj.message, timeout: 3000, showType: 'slide'});
            } else {
                $.messager.alert('提示', obj.message, 'info');
            }
        }
    });
}

function nbcgd_add() {
    $("#cgd_mx").edatagrid('saveRow');
    $("#cgd_fy").edatagrid('saveRow');
    $('#nbcgdForm').tabs('select', "送货单");
    $("#nbcgdForm").form('submit', {
        url: $.webPath() + "/cggl/nbcgd/nbcgd_add",
        onSubmit: function (param) {
            var data = $('#cgd_mx').edatagrid("getData");
            var fydata = $('#cgd_fy').edatagrid("getData");
            if (!data.rows.length) {
                $('#nbcgdForm').tabs('select', "送货明细");
                $.messager.alert("提示", "送货明细需要填写!", "waring");
                return false;
            }
            var arrayPrefix = 'cgd.cgdmxList';
            var fyPrefix = 'cgd.cgfyList';
            //物资计划明细
            var paramString = "{";
            //物资计划明细
            for (var i = 0; i < data.rows.length; i++) {
                if (data.rows[i]['cd.id'] == null || data.rows[i]['cd.id'] == '') {
                    $('#nbcgdForm').tabs('select', "送货明细");
                    $.messager.alert('提示', '第' + (i + 1) + '行的产地不能为空 !', 'info');
                    return false;
                }
//                else if (data.rows[i].js == 0 || data.rows[i].js == null) {
//                    $('#nbcgdForm').tabs('select', "采购明细");
//                    $.messager.alert('提示', '第' + (i + 1) + '行的件数不能为0 !', 'info');
//                    return false;
//                }
                {
                    var objectString = $.toJSONStringSubPrefix(data.rows[i], arrayPrefix + "[" + i + "].",
                        [
                            'id',
                            ['wzgg.id', 'gg.id'],
                            'cd.id',
                            'jzfs.id',
                            'hsj',
                            'bhsj',
                            'bz',
                            'sbsl',
                            'js'
                        ]);
                    paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                }
            }
            //费用物资计划
            for (var i = 0; i < fydata.rows.length; i++) {
                {
                    var objectString = $.toJSONStringPrefix(fydata.rows[i], fyPrefix + "[" + i + "].");
                    paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                }
            }
            if (data.rows && data.rows.length > 0)
                paramString = paramString.substring(0, paramString.length - 1);
            paramString += "}";
            $.extend(param, $.parseJSON(paramString));
            return $(this).form('validate');
        },
        success: function (data) {
            var obj = eval('(' + data + ')');
            if (obj.statusCode == '200') {
                $(nbcgd.win_cgd).window('close');
                $('#grid_nbcgd').datagrid('reload');
                $('#layout_nbcgd').layout('panel', 'center').panel('refresh');
                $.messager.show({title: '提示框', msg: obj.message, timeout: 3000, showType: 'slide'});
            } else {
                $.messager.alert('提示', obj.message, 'info');
            }
        }
    });
}
